package com.fzy.common.date;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期格式转换工具
 *
 * @author 冯赵杨
 */
public class DateUtil2 {

    private DateUtil2() {
    }

    private static Calendar mycd = Calendar.getInstance();
    private static int year = 0;
    private static int month = 0;
    private static int date = 0;

    public DateUtil2(Date nd) {
        mycd.setTime(nd);
        setdate();
    }

    /**
     * 获取当前时间 不带时分秒
     */
    public static String getCurrent() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(new Date());
    }

    /**
     * 获取当前时间 带时分秒
     */
    public static String getCurrentHMS() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date());
    }

    /**
     * Date转成String 带时分秒
     */
    public static String dateToStringHMS(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * Date转成String不带时分秒
     */
    public static String dateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 日期字符串转日期格式 不带时分秒
     */
    public static Date stringToDate(String str) throws ParseException {
        String _pattern = "yyyy-MM-dd";
        return stringToDate(str, _pattern);
    }

    /**
     * 日期字符串转日期格式 带时分秒
     */
    public static Date stringToDateTime(String str) throws ParseException {
        String _pattern = "yyyy-MM-dd HH:mm:ss";
        return stringToDate(str, _pattern);
    }

    /**
     * Date转成String
     */
    public static String dateToString(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * String转成Date
     */
    public static Date stringToDate(String str, String pattern) throws ParseException {
        SimpleDateFormat formater = new SimpleDateFormat(pattern);
        return formater.parse(str);
    }

    /**
     * String转成Timestamp
     */
    public static Timestamp stringToTimestamp(String str) throws Exception {
        Timestamp time = null;
        time = Timestamp.valueOf(str);
        return time;
    }

    /**
     * String转成java.sql.Date对象
     */
    public static java.sql.Date stringToSqlDate(String strDate, String pattern) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = sdf.parse(strDate);
        return new java.sql.Date(date.getTime());
    }

    /**
     * java.sql.Date对象转成String
     */
    public static String sqlDateToString(java.sql.Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 获取返回类型为Timestamp的当前时间
     *
     * @return Timestamp 当前时间
     */
    public static Timestamp getCurrentTime() {
        Timestamp t = new Timestamp(System.currentTimeMillis());
        return t;
    }

    /**
     * 把日期对象转换为字符串日期,格式如: 2011-02-11 12:12:12
     */
    public static String timestamp2String(Timestamp timestamp) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(timestamp);
    }

    /**
     * 得到long型的日期值 *
     */
    public static Time getSqlTime() {
        return new Time(getTime());
    }

    /**
     * 获取系统当前时间 *
     */
    public static long getTime() {
        Date dt = new Date();
        return dt.getTime();
    }

    /**
     * 通过年月日 获取日期类型的日期
     **/
    public static Date ymdToDate(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        return calendar.getTime();
    }

    /**
     * 取指定日后n天，n允许为负数
     */
    public static Date getAfterDay(Date date, int afterDays) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, afterDays);
        return cal.getTime();
    }

    /**
     * 取指定日后n月，n允许为负数
     */
    public static Date getAfterMonth(Date date, int afterMonth) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.MONTH, afterMonth);
        return cal.getTime();
    }

    /**
     * 日期比较返回天
     **/
    public static int DateDiff(Date date1, Date date2) {
        int i = (int) ((date1.getTime() - date2.getTime()) / 3600 / 24 / 1000);
        return i;
    }

    /**
     * 日期比较返回分钟
     **/
    public static int MinDiff(Date date1, Date date2) {
        int i = (int) ((date1.getTime() - date2.getTime()) / 1000 / 60);
        return i;
    }

    /**
     * 日期比较返回秒
     **/
    public static int TimeDiff(Date date1, Date date2) {
        int i = (int) ((date1.getTime() - date2.getTime()));
        return i;
    }

    /**
     * 设置系统时间 *
     */
    private static void setdate() {
        year = mycd.get(Calendar.YEAR);
        month = mycd.get(Calendar.MONTH) + 1;
        date = mycd.get(Calendar.DATE);
        mycd.get(Calendar.HOUR);
        mycd.get(Calendar.MINUTE);
        mycd.get(Calendar.SECOND);
    }

    /**
     * 取得Timestamp类型时间 *
     */
    public static Timestamp getTimestamp() {
        return new Timestamp(getTime());
    }

    /**
     * 得到Calendar对象 *
     */
    public static Calendar getCD() {
        Calendar mycd = Calendar.getInstance();
        return mycd;
    }

    /**
     * 得到时间 *
     */
    public static String getAll(String sStr) {
        Calendar mycd = Calendar.getInstance();
        return mycd.get(Calendar.YEAR) + sStr + (mycd.get(Calendar.MONTH) + 1) + sStr + mycd.get(Calendar.DATE);
    }

    /**
     * 得到日期,以-为分割符 *
     */
    public static String getAll() {
        return getAll("-");
    }

    /**
     * 得到系统当前年 *
     */
    public static int getYear() {
        Calendar mycd = Calendar.getInstance();
        return mycd.get(Calendar.YEAR);
    }

    /**
     * 得到系统当前月 *
     */
    public static int getMonth() {
        Calendar mycd = Calendar.getInstance();
        return mycd.get(Calendar.MONTH) + 1;
    }

    /**
     * 得到系统当前日 *
     */
    public static int getDate() {
        Calendar mycd = Calendar.getInstance();
        return mycd.get(Calendar.DATE);
    }

    /**
     * 得到系统年 *
     */
    public static int getAddYear() {
        return mycd.get(Calendar.YEAR);
    }

    /**
     * 得到系统月 *
     */
    public static int getAddMonth() {
        return mycd.get(Calendar.MONTH) + 1;
    }

    /**
     * 得到系统日 *
     */
    public static int getAddDate() {
        return mycd.get(Calendar.DATE);
    }

    /**
     * 得到日期格式为yyyy-mm-dd *
     */
    public static String getMiddle() {
        return getMiddle("-");
    }

    /**
     * 得到日期格式为YYYY $sStr MM 其中sStr为分割字符 *
     */
    public static String getMiddleYM(String sStr) {
        year = mycd.get(Calendar.YEAR);
        month = mycd.get(Calendar.MONTH) + 1;
        String re = "" + String.valueOf(year);
        if (month < 10) {

            re += sStr + "0" + String.valueOf(month);
        } else {
            re += sStr + String.valueOf(month);
        }
        return re;
    }

    /**
     * 得到日期格式为YYYY $sStr MM $sStr DD其中sStr为分割字符 *
     */
    public static String getMiddle(String sStr) {
        year = mycd.get(Calendar.YEAR);
        month = mycd.get(Calendar.MONTH) + 1;
        date = mycd.get(Calendar.DATE);
        String re = "" + String.valueOf(year);
        if (month < 10) {
            re += sStr + "0" + String.valueOf(month);
        } else {
            re += sStr + String.valueOf(month);
        }
        if (date < 10) {
            re += sStr + "0" + String.valueOf(date);
        } else {
            re += sStr + String.valueOf(date);
        }
        return re;
    }

    /**
     * 通过给定的年、月、日 设置Calendar对象 *
     */
    public DateUtil2(String Year, String Month, String Day) {
        try {
            mycd.set(Integer.parseInt(Year), Integer.parseInt(Month) - 1, Integer.parseInt(Day));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 通过给定的年、月、日 设置Calendar对象 *
     */
    public DateUtil2(int inty, int intm, int intd) {
        mycd.set(inty, intm - 1, intd);
        setdate();
    }

    /**
     * 日期加nday天后，从新设置系统当前时间 *
     */
    public void addDay(int nday) {
        mycd.set(year, month - 1, date + nday);
        setdate();
    }

    /**
     * 日期加nday天后，从新设置系统当前时间 *
     */
    public void addDay(String nday) {
        addDay(Integer.parseInt(nday));
    }

    /**
     * 日期加nmonth月后，从新设置系统当前时间 *
     */
    public void addMonth(int nmonth) {
        mycd.set(year, month + nmonth - 1, date);
        setdate();
    }

    /**
     * 日期加nmonth月后，从新设置系统当前时间 *
     */
    public void addMonth(String nmonth) {
        addMonth(Integer.parseInt(nmonth));
    }

    /**
     * 日期加nyear年后，从新设置系统当前时间 *
     */
    public void addYear(int nyear) {
        mycd.set(year + nyear, month - 1, date);
        setdate();
    }

    /**
     * 日期加nyear年后，从新设置系统当前时间 *
     */
    public void addYear(String nyear) {
        addYear(Integer.parseInt(nyear));
    }

    /**
     * 取得两个日期的相隔天数 *
     */
    public static int dayDiff(Date a, Date b) {
        long temptime = b.getTime() - a.getTime();
        temptime /= 24 * 3600 * 1000;
        return Integer.parseInt(String.valueOf(temptime));
    }

    /**
     * 取得两个日期的相隔天数 *
     */
    public static int getDays(Date sd, Date ed) {
        return (int) ((ed.getTime() - sd.getTime()) / (3600 * 24 * 1000));
    }

    /**
     * 取得yyyymm,参数一：yyyymm,参数二：number 得到减去月份的日期
     **/
    public static String getYearMonth(String str, int num) {
        Calendar mycdar = Calendar.getInstance();
        int yearSub = Integer.parseInt(str.substring(0, 4));
        int monthSub = Integer.parseInt(str.substring(4, 6)) - 1;
        mycdar.set(yearSub, monthSub - num, 1);

        // 月处理
        String monthStr = "";
        monthSub = mycdar.get(Calendar.MONTH) + 1;
        if (monthSub < 10) {
            monthStr = "0" + String.valueOf(monthSub);
        } else {
            monthStr = String.valueOf(monthSub);
        }

        return String.valueOf(mycdar.get(Calendar.YEAR)) + monthStr;
    }

    /**
     * 将String 转换操作，将sDt替换为Timestamp型的日期型
     **/
    public static Timestamp getDateTime(String sDt) {
        try {
            // format:yyyy-mm-dd hh:mm:ss.fffffffff
            return Timestamp.valueOf(sDt);
        } catch (IllegalArgumentException iae) {
            sDt = sDt + " 00:00:00";
            try {
                return Timestamp.valueOf(sDt);
            } catch (Exception e) {
                return null;
            }
        }
    }

    private static String[] CHN_NUM = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};

    /**
     * 显示中文年月
     */
    public static String getCHNYearMonth() {

        StringBuilder str = new StringBuilder(20);
        String year = Calendar.getInstance().get(Calendar.YEAR) + "";
        int month = Calendar.getInstance().get(Calendar.MONTH) + 1;

        for (int i : year.getBytes()) {
            str.append(CHN_NUM[i - 48]);
        }
        str.append("年");
        str.append(CHN_NUM[month]).append("月");

        return str.toString();
    }

    /**
     * @函数名称：toDay @功能描述： 获得格式化后的日期 @return： 格式化后的日期
     */
    public static String toDay() {
        GregorianCalendar thisday = new GregorianCalendar();
        Date d = thisday.getTime();
        DateFormat df = DateFormat.getDateInstance();
        String s = df.format(d);
        return s;
    }

    /**
     * 获取当前时间之前后者之后的日期 (传-1 获取前天的时间)
     */
    public static Date getOtherDate(int offset) {
        Calendar c = Calendar.getInstance();
        c.add(c.DATE, offset);
        Date date = c.getTime();
        return date;
    }

    /**
     * 月份增加
     **/
    public static Date add(Date date, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, amount);
        return c.getTime();
    }

    public static void main(String[] args) {

    }
}
